<?php

namespace App\Http\Controllers\Product;

use App\Http\Controllers\Controller;
use App\Http\Requests\Product\Index\ConsumableRequest;
use App\Http\Requests\Product\Index\RemarkRequest;
use App\Http\Requests\Product\Index\StoreRequest;
use App\Http\Requests\Product\Index\UpdateRequest;
use App\Http\Searchs\Product\IndexSearch;
use App\Repositories\Product;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    /**
     * @var Product
     */
    private $product;

    /**
     * IndexController constructor.
     * @param Product $product
     */
    public function __construct(Product $product)
    {
        $this->product = $product;
    }

    /**
     * Display a listing of the resource.
     *
     * @param IndexSearch $search
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(IndexSearch $search)
    {
        return $this->response(
            $this->product->paginate($search->toArray())
        );
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($request) {
                $this->product->create($request->only([
                    'code', 'customer_id', 'title', 'gross_weight', 'picture', 'brand', 'customs'
                ]));
            })
        );
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id)
    {
        return $this->response(
            $this->product->show($id)
        );
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit($id)
    {
        return $this->response(
            $this->product->edit($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($request, $id) {
                $this->product->update($id, $request->only([
                    'title', 'gross_weight', 'picture', 'brand', 'customs'
                ]));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function review($id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id) {
                $this->product->review($id);
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function confirm($id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id) {
                $this->product->confirm($id);
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function freeze(RemarkRequest $request, $id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id, $request) {
                $this->product->freeze($id, $request->input('remark'));
            })
        );
    }

    /**
     * @param RemarkRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function reject(RemarkRequest $request, $id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id, $request) {
                $this->product->reject($id, $request->input('remark'));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function thaw($id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id) {
                $this->product->thaw($id);
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function consumable($id){
        return $this->response(
            $this->product->consumable($id)
        );
    }

    /**
     * @param ConsumableRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function postConsumable(ConsumableRequest $request, $id){
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($request, $id) {
                $this->product->postConsumable($id, $request->only(['consumables']));
            })
        );
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id) {
                $this->product->delete($id);
            })
        );
    }
}
